var app = getApp()
import Toast from '../../component/vant/toast/toast';
var  fetchApi = require("../../utils/httpPost.js")
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    is_mer: 0,
    is_admin: 0,
    shop_type: 0,
    isLoading: false,
  },
  onLoad: function () {
    var that = this
    var isLogin = app.getStorageSync("isLogin");
    if(isLogin){
      
    }
  },
  onGetUserInfo:function(e){
    var that = this;
    // console.log('onGetUserInfo e',e)
    wx.getUserProfile({
      desc: '用与获取用户信息',
      success:(res)=>{
        console.log('onGetUserInfo ',res)
        that.loginOp(res)
      }
    })
  },
  loginOp: function (res_user) {
    var that = this
    // Toast.fail('loginOp 111')
    that.setData({ isLoading: true})
    app.setStorageSync("isLogin",0);
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log("code", res.code)
        fetchApi.httpPost('/user/wxlogin', { code: res.code }, function (res) {
          console.log("登录返回数据", res)
          if(res.data.code ==0){
            app.globalData.userid = res.data.data.userid
            app.globalData.is_mer = res.data.data.is_mer
            app.globalData.userInfo = res_user.userInfo
            console.log(' res_user.userInfo', res_user.userInfo)

            app.setStorageSync("isLogin",1);
            app.setStorageSync("user_id", res.data.data.userid);
            app.setStorageSync("userInfo", res_user.userInfo);
            app.setStorageSync("is_test", res_user.is_test);
            

            if(res_user.errMsg == 'getUserInfo:fail auth deny'){
              app.setStorageSync("isLogin",0);
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2]; //上一个页面
              console.log('res_user', res_user)
              //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
              prevPage.setData({
                from_page: 'login',
              })
              wx.navigateBack({ delta: 1 });
              // Toast.fail('登录失败11')
            }else{
              fetchApi.httpPost('/user/getUserInfo', {
                iv: res_user.iv,
                encryptedData: res_user.encryptedData,
                rawData: res_user.rawData
              }, function (res_info) {
                console.log('res_info', res_info)
                
                app.setStorageSync("user_type", res_info.data.data.user_type);
                app.setStorageSync("shop_id", res_info.data.data.shop_id);
                app.setStorageSync("shop_name", res_info.data.data.shop_name);
                app.setStorageSync("phone_num", res_info.data.data.phone_num);
                Toast.success('请求user/getUserInfo成功')
            
                var pages = getCurrentPages();
                console.log('pages',pages)
                var prevPage = pages[pages.length - 2]; //上一个页面
                console.log('pages prevPage 1',prevPage)
                //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                prevPage.setData({
                  from_page: 'login',
                })
                console.log('pages prevPage 2 ',prevPage)
                wx.navigateBack({ delta: 1 });
              }, function () {
                // Toast.fail('请求user/getUserInfo失败')
               })
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                console.log('发起回调')
                that.userInfoReadyCallback(res_user)
              }
            }
          }else{
            // Toast.fail('登录失败000')
          }
          that.setData({ isLoading: false, loading: true })
        }, function (res) { 
          // Toast.fail('登录失败222' + res.errMsg)
          that.setData({ isLoading: false, loading: true })
        })
      },fail(res){
        // Toast.fail('登录失败333')
      }
    })
  }
})
